package com.trs.publish.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.trs.publish.domain.Role;
import com.trs.publish.domain.User;

public class SecurityUser extends User implements UserDetails {  
	
    
    public SecurityUser(User user) {  
        if(user != null){
        	this.setId(user.getId());
        	this.setRole(user.getRole());
            this.setUsername(user.getUsername());
            this.setTruename(user.getTruename());
            this.setPassword(user.getPassword());
            this.setSfzh(user.getSfzh());
            this.setDepartment(user.getDepartment());
        }         
    }  
      
    @Override  
    public Collection<? extends GrantedAuthority> getAuthorities() {  
          
        Collection<GrantedAuthority> authorities = new ArrayList<>();  
        Set<Role> userRoles =  new HashSet<Role>();
        String roles = this.getRole();
        if(StringUtils.isNoneEmpty(roles))  {  
            for (String role : roles.split(";")) {  
                GrantedAuthority authority = new SimpleGrantedAuthority(role);  
                authorities.add(authority);  
            }  
        }  
        return authorities;  
    }  
  
    @Override  
    public String getPassword() {  
        return super.getPassword();  
    }  
  
    @Override  
    public String getUsername() {  
        return super.getUsername();  
    }  
  
    @Override  
    public boolean isAccountNonExpired() {  
        return true;  
    }  
  
    @Override  
    public boolean isAccountNonLocked() {  
        return true;  
    }  
  
    @Override  
    public boolean isCredentialsNonExpired() {  
        return true;  
    }  
  
    @Override  
    public boolean isEnabled() {  
        return true;  
    }  
}  